//
//  RightDetailTableViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "RightDetailTableViewController.h"

@interface RightDetailTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) UIImageView *titleImageView;

@property (nonatomic, strong) NSMutableArray *placeDetailArray;

@property (nonatomic, retain) MBProgressHUD *load;

@property (strong, nonatomic) CAGradientLayer *gradientLayer;//渐变

@end

@implementation RightDetailTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-fanhui.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];

#pragma mark -- tableview
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell线

    self.myTableView.scrollsToTop = YES;//快速回到顶部
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[RightDetailTableviewCell class] forCellReuseIdentifier:@"RightDetailTableviewCell"];
    [self.myTableView registerClass:[RightDetailImageCell class] forCellReuseIdentifier:@"RightDetailImageCell"];

#pragma mark -- 下拉方大
    self.myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    self.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, Width, 200 * HEI)];
    self.titleImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.titleImageView.clipsToBounds = YES;
    [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:self.images] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    self.titleImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.myTableView addSubview:self.titleImageView];

    //渐变
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = CGRectMake(0, 0, Width, 200);
    [self.titleImageView.layer addSublayer:self.gradientLayer];

    //设置渐变颜色方向
    self.gradientLayer.startPoint = CGPointMake(0, 1);

    self.gradientLayer.endPoint = CGPointMake(0, 0);
    //设定颜色组
    self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
                                  (__bridge id)[UIColor blackColor].CGColor];

    //设定颜色分割点
    self.gradientLayer.locations = @[@(0.2f) ,@(1.0f)];

#pragma mark -- 上标题
    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake((Width - 300 * WID) / 2, 70 * HEI, 300 * WID, 30 * HEI)];
    nameLable.text = self.name;
    nameLable.textAlignment = NSTextAlignmentCenter;
    nameLable.font = [UIFont systemFontOfSize:25];
    nameLable.textColor = [UIColor whiteColor];
    [self.titleImageView addSubview:nameLable];

#pragma mark -- 线
    UILabel *lLabel = [[UILabel alloc] initWithFrame:CGRectMake((Width - 300 * WID) / 2, 100 * HEI, 300 * WID, 20 * HEI)];
    lLabel.text = @"_______________ . _______________";
    lLabel.textColor = [UIColor whiteColor];
    lLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleImageView addSubview:lLabel];

#pragma mark -- 下标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((Width - 300 * WID) / 2, 130 * HEI, 300 * WID, 30 * HEI)];
    titleLabel.text = self.titlels;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleImageView addSubview:titleLabel];

#pragma mark -- 初始化数组
    self.placeDetailArray = [NSMutableArray array];

    [self getAFNetWorkingData];


#pragma mark -- 菊花
    self.load = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.load show:YES];
    self.load.labelText = @"正在加载";
    [self.view addSubview:self.load];
}

#pragma mark -- 返回
- (void)leftBarButtonItemAction:(UIBarButtonItem *)back{

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 网络请求
- (void)getAFNetWorkingData{

    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/articles/%@.json?page=1",self.pageId];

    [AppTools getDataWithUrlStr:url andBlock:^(id result) {

        NSMutableDictionary *bigDic = result;

        NSMutableArray *array = [bigDic objectForKey:@"article_sections"];

        for (NSMutableDictionary *dic in array) {

            RightDetailModel *placeDetailModel = [[RightDetailModel alloc] init];

            [placeDetailModel setValuesForKeysWithDictionary:dic];

            [self.placeDetailArray addObject:placeDetailModel];
        }

        [self.load hide:YES];//菊花停

        [self.myTableView reloadData];
    }];
}

#pragma mark -- 区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

#pragma mark -- 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.placeDetailArray.count;
}

#pragma mark -- cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RightDetailTableviewCell *picCell = [tableView dequeueReusableCellWithIdentifier:@"RightDetailTableviewCell"];

    RightDetailImageCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"RightDetailImageCell"];

    RightDetailModel *placePlaceModel = [[RightDetailModel alloc] init];

    placePlaceModel = [self.placeDetailArray objectAtIndex:indexPath.row];

    picCell.selectionStyle = UITableViewCellSelectionStyleNone;

    labelCell.selectionStyle = UITableViewCellSelectionStyleNone;

    if ([placePlaceModel.image_url  isEqual: @""]) {

        picCell.placeDetailModel = placePlaceModel;

        return picCell;

    }else{

        labelCell.placeDetailModel = placePlaceModel;

        return labelCell;
    }
}

#pragma mark -- 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    RightDetailModel *placeDetailModel = [[RightDetailModel alloc] init];

    placeDetailModel = [self.placeDetailArray objectAtIndex:indexPath.row];

    CGFloat height;

    if ([placeDetailModel.image_url isEqual:@""]) {

        height = [RightDetailTableviewCell heightForContent:placeDetailModel.detailDescription];

        return height + 60 * HEI;

    }else{

        height = [RightDetailImageCell heightForContent:placeDetailModel.detailDescription] + (Width - 10) / [(placeDetailModel.image_width)floatValue] * [(placeDetailModel.image_height)floatValue];

        return height + 40 * HEI;
    }
}

#pragma mark -- 下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    
    if (y < -200) {
        CGRect frame = self.titleImageView.frame;
        frame.origin.y = y;
        frame.size.height =  -y;

        self.titleImageView.frame = frame;
    }
    
}

@end
